<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="下单用户" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入下单用户" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="item in statusOptions" :key="item.dictValue" :label="item.dictLabel"
            :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="下单时间" style="height:32px;">
        <el-date-picker clearable size="small" style="width: 240px" v-model="dateRange" type="daterange"
          value-format="yyyy-MM-dd" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" align="right"
          unlink-panels :picker-options="pickerOptions" />
      </el-form-item>
      <el-form-item label="客户端" prop="wxClientId" v-has-role="['admin']">
        <el-select v-model="queryParams.wxClientId" placeholder="请选择客户端" clearable size="small">
          <el-option v-for="item in clientList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mall:order:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mall:order:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" width="55" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="下单时间" align="center" prop="createTime" />
      <el-table-column label="付款时间" align="center" prop="orderInfo.payTime" />
      <el-table-column label="实付金额" align="center" prop="orderInfo.payAmount" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="状态" width="100" align="center" prop="statusName" />
      <el-table-column label="退款状态" align="center" prop="refundStatus" :formatter="refundStatusFormat" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:order:query']">详情</el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['mall:order:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/mall/order";
import { checkRole } from "@/utils/permission";
import { listClient } from "@/api/wx/client";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 商城-订单信息表格数据
      orderList: [],
      statusOptions: [{
        dictValue: "0",
        dictLabel: "待付款"
      }, {
        dictValue: "1",
        dictLabel: "已支付"
      }, {
        dictValue: "2",
        dictLabel: "已取消"
      }, {
        dictValue: "7",
        dictLabel: "已退款"
      }, {
        dictValue: "15",
        dictLabel: "已发货"
      }, {
        dictValue: "16",
        dictLabel: "已完成"
      }],
      refundStatusOptions: [{
        dictValue: "0",
        dictLabel: "取消"
      }, {
        dictValue: "1",
        dictLabel: "申请中"
      }, {
        dictValue: "2",
        dictLabel: "已处理"
      }],
      routerOptions: [],
      clientList: [],
      dateRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payOrderId: undefined,
        orderNo: undefined,
        status: undefined,
        wxClientId: undefined,
        wxUserId: undefined,
        num: undefined,
        contactPerson: undefined,
        contactMobile: undefined,
        address: undefined,
        expressCompany: undefined,
        expressNo: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    if (checkRole(['admin'])) {
      listClient().then(response => {
        this.clientList = response.rows;
      });
    }
    this.getDicts("sys_router").then(response => {
      this.routerOptions = response.data;
    });
  },
  methods: {
    /** 查询商城-订单信息列表 */
    getList() {
      this.loading = true;
      listOrder(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    refundStatusFormat(row) {
      return this.selectDictLabel(this.refundStatusOptions, row.refundStatus);
    },
    // 页面路由字典翻译
    routerFormat(row, column) {
      return this.selectDictLabel(this.routerOptions, row);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        payOrderId: undefined,
        orderNo: undefined,
        status: "0",
        delFlag: undefined,
        createTime: undefined,
        updateTime: undefined,
        wxClientId: undefined,
        wxUserId: undefined,
        remark: undefined,
        num: undefined,
        contactPerson: undefined,
        contactMobile: undefined,
        address: undefined,
        expressCompany: undefined,
        expressNo: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      var location = this.routerFormat("OrderInfo");
      // var location = '/mall/order/goods/info';
      this.$router.push({ 'path': location, 'query': { id: id } })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商城-订单信息编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商城-订单信息数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportOrder(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () { });
    }
  }
};
</script>
